// pages/hospitalIntroduction/hospitalIntroduction.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myHttp: app.globalData.myHttp,
    latitude: '0.000000',
    longitude: '0.000000',
    selectedHospitalId: '',
    selectedHospital: '',
    list: [],
    bigList: [], //存放一级科室
    middleList: [], //存放除一级科室外剩余科室
    smallList: [], //存放二级科室
    threeList: [], //存放三级科室
    selectBigDepartment: '',
    selectSmallDepartment: '',
    selectDepartment: '',
    markers: '',
    description: '',
    contents: '',
    hotDoctors: '',
    curIndex: 0,
    curNav: 0,
    navbar: ['科室列表', '医院介绍', '挂号须知'],
    currentTab: 0,
    ellipsis: true, // 文字是否收起，默认收起
  },

  //标签页选择
  navbarTap: function(e) {
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
  },

  //选择大科室
  selectDepartmentOne(event) {
    let id = event.target.dataset.id,
      index = parseInt(event.target.dataset.index);
    self = this;
    this.setData({
      curNav: id,
      curIndex: index,
      selectBigDepartment: this.data.bigList[index],
      selectDepartment: this.data.bigList[index]
    })
  },

  //选择小科室
  selectDepartmentTwo(event) {
    var that = this
    var haveNext = false
    var selectSmallDepartment = event.currentTarget.dataset.department
    if (that.data.threeList != '') {
      console.log("三级集合不为空")
    } else {
      console.log("三级集合为空！！！")
    } 
    if (that.data.threeList != '' && selectSmallDepartment.levels == 2) {
      console.log("点击二级科室展示三级科室:" + selectSmallDepartment)
      for (let i in that.data.threeList) {
        console.log("i为：" + i + "第i个的parentID为：" + that.data.threeList[i].parentID)
        if (that.data.threeList[i].parentID == selectSmallDepartment.id) {
          haveNext = true;
          break
        }
      }
      console.log("haveNext的值为：" + haveNext)
      if (haveNext == false) {
        wx.navigateTo({
          url: '../keshi/keshi?selectSmallDepartment=' + encodeURIComponent(JSON.stringify(selectSmallDepartment))
        })
      } else{
        this.setData({
          selectDepartment: selectSmallDepartment
        })
      }
    } else {
      //硚口妇幼
      if(selectSmallDepartment.hospitalID == '155'){
        wx.navigateTo({
          url: '../qiaokou/qiaokou?divisionId=' + selectSmallDepartment.id
        })
      }else{
        wx.navigateTo({
          url: '../keshi/keshi?selectSmallDepartment=' + encodeURIComponent(JSON.stringify(selectSmallDepartment))
        })
      }
      
    }
  },

  //进入地图
  toMap() {
    wx.navigateTo({
      // url: '../keshi/keshi'
      url: '../map/map?longitude=' + this.data.longitude + '&latitude=' + this.data.latitude + '&hlongitude=' + this.data.selectedHospital.lng + '&hlatitude=' + this.data.selectedHospital.lat,
    })
  },

  //打电话
  freeTell(){
    wx.makePhoneCall({
      phoneNumber: this.data.selectedHospital.phone,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    type: 'wgs84',
      wx.getLocation({
        success: function(res) {
          console.log(res.latitude, res.longitude)
          that.setData({
            latitude: res.latitude,
            longitude: res.longitude
          })
        },
      })
    let selectedHospitalId = JSON.parse(options.selectedHospitalId)
    this.setData({
      selectedHospitalId: selectedHospitalId
    })

    //初始化医院详情
    wx.request({
      url: this.data.myHttp + '/hospital/getHosInfo/' + selectedHospitalId,
      method: "GET",
      header: {
        'content-type': 'application/json',
      },
      success(res) {
        that.setData({
          description: app.convertHtmlToText(res.data.obj.description),
          contents: app.convertHtmlToText(res.data.obj.contents),
          selectedHospital: res.data.obj
        })
      },
    })


    //初始化科室列表数据
    wx.request({
      url: this.data.myHttp + '/hospital/divisionList',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        "hId": selectedHospitalId
      },
      success(res) {
        var list = []
        for (let i in res.data.obj) {
          list.push(res.data.obj[i])
        }
        var bigList = []
        var middleList = []
        var smallList = []
        var threeList = []
        for (let i = 0; i < list.length; i++) {
          if (list[i].parentID == 0) {
            bigList.push(list[i])
          } else {
            middleList.push(list[i])
          }
        }
        if (middleList != '' && middleList[0].levels == null) {
          for (let i = 0; i < middleList.length; i++) {
            smallList[i] = middleList[i]
          }
        } else {
          for (let i = 0; i < middleList.length; i++) {
            if (middleList[i].levels == 3) {
              threeList[i] = middleList[i]
            } else {
              smallList[i] = middleList[i]
            }
          }
        }

        //   if (list[i].levels == 1) {
        //     bigList.push(list[i])
        //   } else {
        //     if (list[i].levels == 2) {
        //       smallList.push(list[i])
        //     } else {
        //       if (list[i].levels == 3) {
        //         threeList.push(list[i])
        //       }
        //     }
        //   }
        // }

        that.setData({
          list: list,
          smallList: smallList,
          middleList:middleList,
          bigList: bigList,
          selectBigDepartment: bigList[0],
          selectDepartment: bigList[0],
          threeList: threeList
        })
      }
    })


    //初始化热门医生推荐
    // wx.request({
    //   url: this.data.myHttp + '/inquiry/getHotDivisionAndDoc',
    //   method: "GET",
    //   header: {
    //     'content-type': 'application/json',
    //   },
    //   success(res) {
    //     that.setData({
    //       hotDoctors: res.data.obj.hotdocs
    //     })
    //   }
    // })
  },

  //点击热门医生进入该医生详情页面
  toHotDoctor(event) {
    var thisHotDoctor = event.currentTarget.dataset.item
    wx.navigateTo({
      url: '../doctordetails/doctordetails?selectyisheng=' + encodeURIComponent(JSON.stringify(thisHotDoctor)),
    })
  },


  //图片加载失败处理
  //图片加载失败使用同一图片代替
  binderrorimg: function (e) {
    var errorImgIndex = e.target.dataset.errorimg //获取循环的小标
    var imgObject = "selectedHospital.middleLogo" //carlistData为数据源，对象数组
    var errorImg = {}
    errorImg[imgObject] = "../../image/hospital.png" //我们构建一个对象
    this.setData(errorImg) //修改数据源对应的数据
  },
  /**
   * 收起/展开按钮点击事件
   */
  ellipsis: function () {  
    var value = !this.data.ellipsis;
    this.setData({
      ellipsis: value
    }) 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})